home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / CShell⁄THINK C / DoEvent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-20  |  7.8 KB  |  352 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:        CShell
  5. ** File:        doevent.c
  6. ** Written by:  Eric Soldan
  7. **
  8. ** Copyright © 1990-1991 Apple Computer, Inc.
  9. ** All rights reserved.
  10. */
  11.  
  12.  
  13.  
  14. /*****************************************************************************/
  15.  
  16.  
  17.  
  18. #include "CShell.h"                /* Get the CShell includes/typedefs, etc.    */
  19. #include "CShellCommon.h"        /* Get the stuff in common with rez.        */
  20. #include "CShell.protos"        /* Get the prototypes for CShell.            */
  21.  
  22. #ifndef __DESK__
  23. #include <Desk.h>
  24. #endif
  25.  
  26. #ifndef __DISKINIT__
  27. #include <DiskInit.h>
  28. #endif
  29.  
  30. #ifndef __ERRORS__
  31. #include <Errors.h>
  32. #endif
  33.  
  34. #ifndef __MENUS__
  35. #include <Menus.h>
  36. #endif
  37.  
  38. #ifndef __TEXTEDITCONTROL__
  39. #include "TextEditControl.h"
  40. #endif
  41.  
  42. #ifndef __TOOLUTILS__
  43. #include <ToolUtils.h>
  44. #endif
  45.  
  46. #ifndef __UTILITIES__
  47. #include "Utilities.h"
  48. #endif
  49.  
  50.  
  51.  
  52. /*****************************************************************************/
  53.  
  54.  
  55.  
  56. extern Cursor    *gCurrentCursor;
  57.  
  58. static void        ResizeContent(WindowPtr window);
  59.  
  60.  
  61.  
  62. /*****************************************************************************/
  63. /*****************************************************************************/
  64.  
  65.  
  66.  
  67. /* Do the right thing for an event.  Determine what kind of event it is, and
  68. ** call the appropriate routines.
  69. */
  70.  
  71. #pragma segment Main
  72. void    DoEvent(EventRecord *event)
  73. {
  74.     short            part, err;
  75.     WindowPtr        window;
  76.     char            key;
  77.     Point            pt;
  78.     Rect            contentRct, oldRct, growLimits;
  79.     long            size;
  80.     FileRecHndl        frHndl;
  81.  
  82.     switch(event->what) {
  83.  
  84.         case mouseDown:
  85.             gCurrentCursor = nil;
  86.                 /* No shortcuts when we recalculate the cursor region. */
  87.  
  88.             part = FindWindow(event->where, &window);
  89.             if (part != inContent) DoSetCursor(&QD(arrow));
  90.  
  91.             switch(part) {
  92.  
  93.                 case inContent:
  94.                     if (window != FrontWindow()) {
  95.                         SelectWindow(window);
  96.                         if (IsAppWindow(window)) {
  97.                             DoUpdate(window);
  98.                             contentRct = GetWindowContentRect(window);
  99.                             if (PtInRect(event->where, &contentRct))
  100.                                 DoEvent(event);
  101.                         }
  102.                                     /* Do first click if over board. */
  103.                     } else
  104.                         DoContentClick(window, event);
  105.                     break;
  106.  
  107.                 case inDrag:            
  108.                     DragWindow(window, event->where, &QD(screenBits.bounds));
  109.                     break;        /* Pass screenBits.bounds to
  110.                                 ** get all gDevices. */
  111.  
  112.                 case inGoAway:
  113.                     if (TrackGoAway(window, event->where)) {
  114.                         DisposeOneWindow(window, iClose);
  115.                     }
  116.                     break;
  117.  
  118.                 case inGrow:
  119.                     oldRct = GetWindowContentRect(window);
  120.                     SetRect(&growLimits, kMinWindowWidth, kMinWindowHeight,
  121.                                          kMaxWindowWidth, kMaxWindowHeight);
  122.                     if (size = GrowWindow(window, event->where, &growLimits)) {
  123.                         pt = *(Point *)&size;
  124.                         SizeWindow(window, pt.h, pt.v, true);
  125.                         ResizeContent(window);
  126.                     }
  127.                     break;
  128.  
  129.                 case inMenuBar:        /* Process mouse menu command (if any). */
  130.                     AdjustMenus();
  131.                     DoMenuCommand(MenuSelect(event->where));
  132.                     break;
  133.  
  134.                 case inSysWindow:    /* Let the system handle the mouseDown. */
  135.                     SystemClick(event, window);
  136.                     break;
  137.  
  138.                 case inZoomIn:
  139.                 case inZoomOut:
  140.                     if (TrackBox(window, event->where, part)) {
  141.                         ZoomToWindowDevice(window, 640, 400, part, true);
  142.                         ResizeContent(window);
  143.                     }
  144.                     break;
  145.  
  146.             }
  147.             break;
  148.  
  149.         case activateEvt:
  150.             gCurrentCursor = nil;
  151.                 /* No shortcuts when we recalculate the cursor region. */
  152.  
  153.             DoActivate((WindowPtr)event->message,
  154.                        (event->modifiers & activeFlag));
  155.             break;
  156.  
  157.         case autoKey:
  158.         case keyDown:                    /* Check for menukey equivalents. */
  159.             key = event->message & charCodeMask;
  160.             if (event->modifiers & cmdKey) {        /* Command key down. */
  161.                 if (event->what == keyDown) {
  162.                     AdjustMenus();
  163.                         /* Enable/disable/check menu items properly. */
  164.                     DoMenuCommand(MenuKey(key));
  165.                 }
  166.                 break;
  167.             }
  168.  
  169.             if (!IsAppWindow(window = FrontWindow())) break;
  170.             frHndl = (FileRecHndl)GetWRefCon(window);
  171.  
  172.             if (key == 0x03) {
  173.                 SendMessage(frHndl, kTextMssg);
  174.                 break;
  175.             }
  176.             if (CTEKey(event)) {
  177.                 (*frHndl)->fileState.docDirty = true;
  178.                 AdjustMenus();        /* Avoid unnecessary DoCursor() and speed */
  179.                 return;                /* up TextEdit entry. */
  180.             }
  181.             break;
  182.  
  183.         case diskEvt:
  184.             gCurrentCursor = nil;
  185.                 /* No shortcuts when we recalculate the cursor region. */
  186.  
  187.             if (HiWord(event->message) != noErr) {
  188.                 SetPt(&pt, kDILeft, kDITop);
  189.                 err = DIBadMount(pt, event->message);
  190.             }
  191.             break;        /* It is not a bad idea to at least call DIBadMount
  192.                         ** in response to a diskEvt, so that the user can
  193.                         ** format a floppy.
  194.                         */
  195.         case kHighLevelEvent:
  196.             gCurrentCursor = nil;
  197.                 /* No shortcuts when we recalculate the cursor region. */
  198.  
  199.             DoHighLevelEvent(event);
  200.             break;
  201.  
  202.         case kOSEvent:
  203.             gCurrentCursor = nil;
  204.                 /* No shortcuts when we recalculate the cursor region. */
  205.  
  206.             switch ((event->message >> 24) & 0x0FF) {
  207.                     /* Must logical and with 0x0FF to get only low byte. */
  208.                     /* High byte of message. */
  209.  
  210.                 case kMouseMovedMessage:
  211.                     break;
  212.  
  213.                 case kSuspendResumeMessage:
  214.                         /* Suspend/resume is also an activate/deactivate. */
  215.                     gInBackground = !(event->message & kResumeMask);
  216.                     DoActivate(FrontWindow(), !gInBackground);
  217.                     break;
  218.             }
  219.             break;
  220.  
  221.         case updateEvt:
  222.             DoUpdate((WindowPtr)event->message);
  223.             break;
  224.  
  225.     }
  226.  
  227.     DoCursor(false, 0);
  228.     AdjustMenus();
  229. }
  230.  
  231.  
  232.  
  233. /*****************************************************************************/
  234.  
  235.  
  236.  
  237. /* This is called when a window is activated or deactivated. */
  238.  
  239. #pragma segment Main
  240. void    DoActivate(WindowPtr window, Boolean becomingActive)
  241. {
  242.     NotifyCancel();
  243.  
  244.     if (IsAppWindow(window)) {
  245.         SetPort(window);
  246.         CTEWindActivate(window, becomingActive);
  247.         DoDrawGrowIcon(window, false, false);    /* Just draw the icon. */
  248.         DoDrawControls(window, true);            /* Redraw just the scrollbars. */
  249.         SetOrigin(0, 0);
  250.     }
  251. }
  252.  
  253.  
  254.  
  255. /*****************************************************************************/
  256.  
  257.  
  258.  
  259. /* This is called when an update event is received for a window.  It calls
  260. ** ImageDocument to draw the contents of an application window.  As an
  261. ** effeciency measure that does not have to be followed, it calls the drawing
  262. ** routine only if the visRgn is non-empty.  This will handle situations where
  263. ** calculations for drawing or drawing itself is very time-consuming.
  264. */
  265.  
  266. #pragma segment Main
  267. void    DoUpdate(WindowPtr window)
  268. {
  269.     if (IsAppWindow(window)) {
  270.         BeginUpdate(window);                /* This sets up the visRgn. */
  271.         if (!EmptyRgn(window->visRgn)) {    /* Draw if updating needs doing. */
  272.             SetPort(window);
  273.             ImageDocument((FileRecHndl)GetWRefCon(window));
  274.         }
  275.         EndUpdate(window);
  276.     }
  277. }
  278.  
  279.  
  280.  
  281. /*****************************************************************************/
  282.  
  283.  
  284.  
  285. /* This is called when a mouse-down event occurs in the content of a window.
  286. ** Other applications might want to call FindControl, TEClick, etc., to
  287. ** further process the click.
  288. */
  289.  
  290. #pragma segment Main
  291. void    DoContentClick(WindowPtr window, EventRecord *event)
  292. {
  293.     Point        clickLoc;
  294.     FileRecHndl    frHndl;
  295.  
  296.     if (!IsAppWindow(window)) return;
  297.  
  298.     SetPort(window);
  299.  
  300.     clickLoc = event->where;
  301.     GlobalToLocal(&clickLoc);
  302.  
  303.     if (CTEClick(event)) return;
  304.         /* If TextEdit control handled the click, we are done. */
  305.  
  306.     frHndl = (FileRecHndl)GetWRefCon(window);
  307.     if ((*frHndl)->fileState.readOnly) return;
  308.         /* Don't allow changes if read-only. */
  309. }
  310.  
  311.  
  312.  
  313. /*****************************************************************************/
  314.  
  315.  
  316.  
  317. #pragma segment Main
  318. void    ResizeContent(WindowPtr window)
  319. {
  320.     FileRecHndl    frHndl;
  321.     WindowPtr    oldPort;
  322.     short        i;
  323.     Rect        rct;
  324.     TEHandle    teHndl;
  325.  
  326.     frHndl  = (FileRecHndl)GetWRefCon(window);
  327.     oldPort = SetFilePort(frHndl);
  328.  
  329.     for (i = 0; i < 2; ++i) {
  330.         rct = window->portRect;
  331.         --rct.top;
  332.         --rct.left;
  333.         rct.right -= 14;
  334.         rct.bottom = rct.top + (rct.bottom - rct.top + 1) / 2;
  335.         if (i) {
  336.             rct.top = rct.bottom - 1;
  337.             rct.bottom = window->portRect.bottom + 1;
  338.         }
  339.  
  340.         teHndl = (*frHndl)->doc.inBox;
  341.         if (i) teHndl = (*frHndl)->doc.outBox;
  342.  
  343.         CTESize(teHndl, rct.right - rct.left, rct.bottom - rct.top);
  344.         CTEMove(teHndl, rct.left, rct.top);
  345.     }
  346.  
  347.     SetPort(oldPort);
  348. }
  349.  
  350.  
  351.  
  352.